<?php

namespace app\admin\controller\hotel;

use app\common\controller\Backend;

class Room extends Backend
{
    public function __construct(){
        parent::__construct();

        $this->RoomModel = model('Hotel.room');
    }

    // 房间列表
    public function index(){
        $this->request->filter(['strip_tags','trim']);
        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            // 表格的两个返回值
            $total = $this->RoomModel
                ->where($where)
                ->limit($offset,$limit)
                ->count();

            $list = $this->RoomModel
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }


    // 添加房间
    public function add(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;
            $params = $this->request->param('row/a');

            $data = [
                'name' => $params['name'],
                'flag' => $params['flag'],
                'service' => $params['service'],
                'price' => $params['price'],
                'total' => $params['total'],
                'thumb' => $params['thumb'],
                'live' => $params['live'],
                'area' => $params['area']
            ];

            $result = $this->RoomModel->validate('common/Hotel/Room')->save($data);
            
            if($result === FALSE){
                $this->error('添加失败');
                exit;
            }else{
                $this->success('添加成功');
                exit;
            }


        }

        return $this->view->fetch();
    }


    // 编辑
    public function edit($ids=NULL){
        $row = $this->RoomModel->find($ids);
        if($this->request->isPost()){
            $params = $this->request->param('row/a');

            $data = [
                'id' => $ids,
                'name' => $params['name'],
                'thumb' => $params['thumb'],
                'price' => $params['price'],
                'flag' => $params['flag'],
                'total' => $params['total'],
                'service' => $params['service'],
                'live' => $params['live'],
                'area' => $params['area']
            ];

            $result = $this->RoomModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('编辑失败');
                exit;
            }else{
                if($data['thumb'] != $row['thumb']){
                    is_file('.'.$row['thumb']) && @unlink('.'.$row['thumb']);

                }
                $this->success('编辑成功');
                exit;
            }
        }
        $this->assign('row',$row);
        return $this->view->fetch();
    }


    // 删除
    public function del($ids=NULL){
        if($this->request->isPost()){
            $room = $this->RoomModel->find($ids);

            if(!$room){
                $this->error('房间未知');
                exit;
            }

            $result = $this->RoomModel->destroy($ids);

            if($result === FALSE){
                $this->error('删除失败');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }


    // 回收站
    public function recyclebin(){
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->RoomModel
                ->onlyTrashed()
                ->where($where)
                ->count();

            $list = $this->RoomModel
                ->onlyTrashed()
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }


}
